<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class MidiclubController extends BaseController {

    //机构列表
	public function index() {
        //筛选条件
        $data = array_filter($_GET);
        if (!empty($_GET['midiclub_name'])) {
            $data['midiclub_name'] = array('like',"%".trim($_GET['midiclub_name'])."%");
        }
        if($data['midiclub_type'] == 1){
            $data['midiclub_type'] = array('IN','1,3');
        }else if($data['midiclub_type'] == 2){
            $data['midiclub_type'] = array('IN','2,3');
        }
        unset($data['p']);
        $data['midiclub_display'] = 1;

        $count = M('midiclub')->alias('m')->join('left join midi_city as c on m.midiclub_city = c.city_id')->where($data)->count();
        $page = getpage($count,10);
        $midiclub_arr = M('midiclub')->alias('m')->join('left join midi_city as c on m.midiclub_city = c.city_id')->where($data)->order('midiclub_id desc')->limit($page->firstRow, $page->listRows)->select();
        $city_arr = M('city')->select();
        $this->assign('city_arr', $city_arr);
        $this->assign('midiclub_arr', $midiclub_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //添加机构
    public function midiclub_add(){
        if (I('post.action') == 'midiclub_add_action') {
            $this->midiclub_add_action();
            exit();
        }

        $sql = "select * from midi_city order by CONVERT( city_name USING gbk ) COLLATE gbk_chinese_ci ASC";
        $citys = M()->query($sql);
        $this->assign('citys',$citys);
        $this->display();
    }

    //添加机构处理
    public function midiclub_add_action(){

        if($_POST != array_filter($_POST) ){
            $this->error('选项中不能有空值');
        }

        $finfo = upload($_FILES['pic']);
        if ($finfo['status'] == 0) {
            if ($finfo['msg'] == 'nofile') {
                $this->error('图片未上传');
            }
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $data['midiclub_pic'] = $finfo['filepath'];
        }

        $data['midiclub_name'] = $_POST['midiclub_name'];
        $data['midiclub_phone'] = $_POST['midiclub_phone'];
        $data['midiclub_address'] = $_POST['midiclub_address'];
        $data['midiclub_content'] = $_POST['midiclub_content'];
        $data['midiclub_lng'] = $_POST['midiclub_lng'];
        $data['midiclub_lat'] = $_POST['midiclub_lat'];
        $data['midiclub_city'] = $_POST['midiclub_city'];
        $data['midiclub_type'] = $_POST['midiclub_type'];
        $data['midiclub_level'] = $_POST['midiclub_level'];
        if(M('midiclub')->add($data)){
            $this->redirect('index','添加机构成功');
        }else{
            $this->error('添加机构失败');
        }
    }

    //删除机构
    public function midiclub_del(){
        $data['midiclub_id'] = I('get.midiclub_id');
        $data['midiclub_display'] = 0;
        if(M('midiclub')->save($data)){
            $this->redirect('index','删除机构成功');
        }else{
            $this->error('删除机构失败');
        }
    }

    //机构详情
    public function midiclub_details(){
        $data['midiclub_id'] = I('get.midiclub_id');
        $midiclub_arr = M('midiclub')->alias('m')->join('left join midi_city as c on m.midiclub_city = c.city_id')->where($data)->find();
        $this->assign('midiclub_arr',$midiclub_arr);
        $this->display();
    }

    //修改机构信息
    public function midiclub_edit(){
        if (I('post.action') == 'midiclub_edit_action') {
            $this->midiclub_edit_action();
            exit();
        }
        $data['midiclub_id'] = I('get.midiclub_id');
        $midiclub_arr = M('midiclub')->where($data)->find();
        $this->assign('midiclub_arr',$midiclub_arr);
        $sql = "select * from midi_city order by CONVERT( city_name USING gbk ) COLLATE gbk_chinese_ci ASC";
        $citys = M()->query($sql);
        $this->assign('citys',$citys);
        $this->display();
    }

    //修改机构信息操作
    public function midiclub_edit_action(){

        $finfo = upload($_FILES['pic']);
        if ($finfo['status'] == 0 && $finfo['msg'] != 'nofile') {
            $this->error($finfo['msg']);
        } elseif ($finfo['status'] == 1) {
            $_POST['midiclub_pic'] = $finfo['filepath'];
        }

        if(M('midiclub')->save($_POST)){
            $this->success('修改信息成功');
        }else{
            $this->error('没有信息被修改');
        }
    }

}